package com.haochang.chunkvod.view.vod.activity;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseEditText;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.model.custom.CustomDialogData;
import com.haochang.chunkvod.view.vod.dialog.DialogItemInfo;
import com.haochang.chunkvod.view.vod.dialog.KeyBoardDialog;

import android.content.Intent;
import android.os.Parcelable;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

/**
 *@author qintao
 *created at 2016/8/4 10:39
 * 自定义单行输入的activity--dialog样式
 */

public class SingleInputActivity extends BaseActivity implements KeyBoardDialog.KeyBoardOnSureListener
{
	private BaseTextView tv_title;
	private BaseEditText tv_content;
	private Button btn_cancel;
	private Button btn_sure;
	private String btnSureText;
	private String btnCancelText;
	private String titleText;
	private String contentText;
	private String hintText;
	private String billId;
	private DialogItemInfo dialogItemInfo;
	private ActivityEnum activityEnum = ActivityEnum.NORMAL;

	@Override
	public void receiveParam()
	{
		titleText = getIntent().getStringExtra(IntentKey.CUSTOM_DIALOG_TITLE);
		hintText = getIntent().getStringExtra(IntentKey.CUSTOM_DIALOG_CONTENT_HINT);
		contentText = getIntent().getStringExtra(IntentKey.CUSTOM_DIALOG_CONTENT);
		btnSureText = getIntent().getStringExtra(IntentKey.BTN_SURE);
		btnCancelText = getIntent().getStringExtra(IntentKey.BTN_CANCEL);
		billId = getIntent().getStringExtra(IntentKey.BILL_ID);
		dialogItemInfo = (DialogItemInfo) getIntent().getSerializableExtra(IntentKey.DIALOGITEMINFO);
		activityEnum = (ActivityEnum) getIntent().getSerializableExtra(IntentKey.ACTIVITY_ENUM);
	}

	@Override
	public void initData()
	{

	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.common_dialog_activity_single);
		tv_title = (BaseTextView) findViewById(R.id.tv_title);
		tv_content = (BaseEditText) findViewById(R.id.tv_content);
		btn_cancel = (Button) findViewById(R.id.btn_cancel);
		btn_sure = (Button) findViewById(R.id.btn_sure);
		tv_title.setText(titleText);
		if (!TextUtils.isEmpty(hintText))
			tv_content.setHint(hintText);
		else
			tv_content.setText(contentText);
		btn_cancel.setText(btnCancelText);
		btn_sure.setText(btnSureText);
		btn_sure.setOnClickListener(mOnBaseClickListener);
		btn_cancel.setOnClickListener(mOnBaseClickListener);
		if (activityEnum == ActivityEnum.PERMISSION)
		{
			tv_content.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
		} else if (activityEnum == ActivityEnum.ROOM_NUMBERS)
		{
			tv_content.setFocusable(false);
			tv_content.setOnClickListener(mOnBaseClickListener);
		}
	}

	private OnBaseClickListener mOnBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.btn_sure:
				String content = tv_content.getText().toString().trim();
				if (TextUtils.isEmpty(content) && activityEnum == ActivityEnum.ROOM_NUMBERS)
				{
					ToastUtils.showText(SingleInputActivity.this, "人数不可为空");
				} else if (TextUtils.isEmpty(content) && (activityEnum == ActivityEnum.PERMISSION || activityEnum == ActivityEnum.SPECIAL_PERMISSION))
				{
					ToastUtils.showText(SingleInputActivity.this, "验证密码不能为空");
				} else
				{
					Intent intent = new Intent();
					intent.putExtra(IntentKey.CUSTOM_DIALOG_CONTENT, content);
					if (activityEnum == ActivityEnum.ROOM_NUMBERS)
					{
						intent.putExtra(IntentKey.BILL_ID, billId);
					} else if (activityEnum == ActivityEnum.SPECIAL_PERMISSION)
					{
						intent.putExtra(IntentKey.DIALOGITEMINFO, (Parcelable) dialogItemInfo);
					}
					setResult(RESULT_OK, intent);
					finish();
				}
				break;
			case R.id.btn_cancel:
				finish();
				break;
			case R.id.tv_content:
				KeyBoardDialog keyBoardDialog = new KeyBoardDialog(SingleInputActivity.this);
				keyBoardDialog.setKeyBoardOnSureListener(SingleInputActivity.this);
				keyBoardDialog.show();
				break;
			default:
				break;
			}
		}
	};

	@Override
	public void setValue(String value)
	{
		if (value.contains("."))
			ToastUtils.showText(SingleInputActivity.this, R.string.error_number);
		else if (value.length() > 9)
			ToastUtils.showText(SingleInputActivity.this, R.string.error_max_number);
		else if (Integer.parseInt(value) <= 0)
			ToastUtils.showText(SingleInputActivity.this, R.string.error_number);
		else
		{
			tv_content.setText(value);
		}
	}

	public enum ActivityEnum
	{
		NORMAL,
		/**
		 * 权限验证框
		 */
		PERMISSION,
		/**
		 * 特殊收款
		 */
		SPECIAL_PERMISSION,
		/**
		 * 房间人数
		 */
		ROOM_NUMBERS;
	}

}
